#include <QtGui>
#include "RadianEditor.h"
#include "RadianEditorEngine.h"
#include "RadianMainRenderWnd.h"
#include "RadianEditorController.h"
//--------------------------------------------------------------------------
template<> RadianEditor* Ogre::Singleton<RadianEditor>::ms_Singleton = 0;
//--------------------------------------------------------------------------
RadianEditor::RadianEditor()
	:QMainWindow(NULL)
	,m_RadianMainRenderWnd(NULL)
{
	Ui::MainWindow::setupUi(this);
	init();
	m_RadianMainRenderWnd = new RadianMainRenderWnd(frameRenderArea);
	m_RadianMainRenderWnd->Initialize();

	frameRenderArea->setMouseTracking(true);
	m_EitorTimer = new QTimer(this);

	m_RadianResourceBrowerWnd = NULL;
	connect(m_EitorTimer,SIGNAL(timeout()),this,SLOT(OnTick()));

}
//--------------------------------------------------------------------------
RadianEditor::~RadianEditor()
{

}
//--------------------------------------------------------------------------
void RadianEditor::init()
{
	setCentralWidget(frameRenderArea);

	createActions();
	createMenus();
	createToolBars();
	createStatusBar();

}
//--------------------------------------------------------------------------
void RadianEditor::ShowEditor()
{

	this->show();
	//RadianEditorEngine::getSingletonPtr()->Run();
	m_RadianMainRenderWnd->SceneSetup();
	m_EitorTimer->start(G_Editor_TickTime);
}
//--------------------------------------------------------------------------
void RadianEditor::newFile()
{
	
}
//--------------------------------------------------------------------------
void RadianEditor::open()
{
	
}
//--------------------------------------------------------------------------
void RadianEditor::closeEvent(QCloseEvent *event)
{
	event->accept();
}
//--------------------------------------------------------------------------
bool RadianEditor::save()
{
	return true;
}
QWidget* RadianEditor::GetRenderAreaID()
{
	return frameRenderArea;
}
//--------------------------------------------------------------------------
void RadianEditor::createActions()
{
	newAct = new QAction(QIcon(":/RadianEditor/new.png"), tr("&New"), this);
	newAct->setShortcuts(QKeySequence::New);
	newAct->setStatusTip(tr("Create a new file"));
	connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));

	openAct = new QAction(QIcon(":/RadianEditor/open.png"), tr("&Open..."), this);
	openAct->setShortcuts(QKeySequence::Open);
	openAct->setStatusTip(tr("Open an existing file"));
	connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

	saveAct = new QAction(QIcon(":/RadianEditor/save.png"), tr("&Save"), this);
	saveAct->setShortcuts(QKeySequence::Save);
	saveAct->setStatusTip(tr("Save the document to disk"));
	connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

	closeAct = new QAction(tr("&Close"), this);
	closeAct->setShortcut(tr("Ctrl+W"));
	closeAct->setStatusTip(tr("Close this window"));
	connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));

	exitAct = new QAction(tr("E&xit"), this);
	exitAct->setShortcuts(QKeySequence::Quit);
	exitAct->setStatusTip(tr("Exit the application"));
	connect(exitAct, SIGNAL(triggered()), qApp, SLOT(exit()));

	cutAct = new QAction(QIcon(":/RadianEditor/cut.png"), tr("Cu&t"), this);
	cutAct->setShortcuts(QKeySequence::Cut);
	cutAct->setStatusTip(tr("Cut the current selection's contents to the "
		"clipboard"));

	copyAct = new QAction(QIcon(":/RadianEditor/copy.png"), tr("&Copy"), this);
	copyAct->setShortcuts(QKeySequence::Copy);
	copyAct->setStatusTip(tr("Copy the current selection's contents to the "
		"clipboard"));

	pasteAct = new QAction(QIcon(":/RadianEditor/paste.png"), tr("&Paste"), this);
	pasteAct->setShortcuts(QKeySequence::Paste);
	pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
		"selection"));

	aboutAct = new QAction(tr("&About"), this);
	aboutAct->setStatusTip(tr("Show the application's About box"));
	connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

	aboutQtAct = new QAction(tr("About &Qt"), this);
	aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
	connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
	
	resouceViewAct= new QAction(QIcon(":/RadianEditor/ResourceView"),tr("&ResourceEditor"), this);
	resouceViewAct->setStatusTip(tr("Show the ResourceView window"));
	connect(resouceViewAct, SIGNAL(triggered() ), this, SLOT(displayResourceBrower()));

	realtimeAct = new QAction(QIcon(":/RadianEditor/Realtime"),tr("&RealTime"), this);
	realtimeAct->setStatusTip(tr("Switch to RealTime mode"));
	connect(realtimeAct, SIGNAL(triggered() ), this, SLOT(switchToRealTimeMode()));

	cutAct->setEnabled(false);
	copyAct->setEnabled(false);

}
//--------------------------------------------------------------------------
void RadianEditor::createMenus()
{
	fileMenu = menuBarMain->addMenu(tr("&File"));
	//! [implicit tr context]
	fileMenu->addAction(newAct);
	fileMenu->addAction(openAct);
	fileMenu->addAction(saveAct);
	fileMenu->addSeparator();
	fileMenu->addAction(closeAct);
	fileMenu->addAction(exitAct);

	editMenu = menuBarMain->addMenu(tr("&Edit"));
	editMenu->addAction(cutAct);
	editMenu->addAction(copyAct);
	editMenu->addAction(pasteAct);

	menuBarMain->addSeparator();

	helpMenu = menuBarMain->addMenu(tr("&Help"));
	helpMenu->addAction(aboutAct);
	helpMenu->addAction(aboutQtAct);

	toolsMenu = menuBarMain->addMenu(tr("&Tools"));
	toolsMenu->addAction(resouceViewAct);
	toolsMenu->addAction(realtimeAct);
}
//--------------------------------------------------------------------------
void RadianEditor::createToolBars()
{
	//! [0]
	fileToolBar = addToolBar(tr("File"));
	fileToolBar->addAction(newAct);
	fileToolBar->addAction(openAct);
	fileToolBar->addAction(saveAct);

	editToolBar = addToolBar(tr("Edit"));
	editToolBar->addAction(cutAct);
	editToolBar->addAction(copyAct);
	editToolBar->addAction(pasteAct);

	viewToolBar = addToolBar(tr("View"));
	viewToolBar->addAction(resouceViewAct);
	viewToolBar->addAction(realtimeAct);
}
//--------------------------------------------------------------------------
QString RadianEditor::strippedName(const QString &fullFileName)
{
	return QFileInfo(fullFileName).fileName();
}
//--------------------------------------------------------------------------
void RadianEditor::createStatusBar()
{
	statusBar()->showMessage(tr("Ready"));
}
//--------------------------------------------------------------------------
void RadianEditor::about()
{
	QMessageBox::about(this, tr("About SDI"),
		tr("Radian Editor."));
}
//--------------------------------------------------------------------------
RadianEditor* RadianEditor::getSingletonPtr()
{
	assert(ms_Singleton); return ms_Singleton;
}
//--------------------------------------------------------------------------
RadianEditor& RadianEditor::getSingleton()
{
	return *ms_Singleton;
}
//--------------------------------------------------------------------------
void RadianEditor::OnTick()
{
	m_RadianMainRenderWnd->Update(G_Editor_TickTime,RadianEditorEngine::getSingleton().GetIsRealTimeMode());
	RadianEditorEngine::getSingleton().TickEngine();
}
//--------------------------------------------------------------------------
//void RadianEditor::resizeEvent(QResizeEvent* evt)
//{
//	const QSize& currentSize = evt->size();
//
//	if(m_RadianMainRenderWnd)
//		m_RadianMainRenderWnd->WindowResize(currentSize.width(),currentSize.height());
//
//}
//--------------------------------------------------------------------------
void RadianEditor::displayResourceBrower()
{
	if (NULL == m_RadianResourceBrowerWnd)
	{
		m_RadianResourceBrowerWnd = new RadianResourceBrowser();
	}
	m_RadianResourceBrowerWnd->resourceTree->clear();
	m_RadianResourceBrowerWnd->ReadDir();

	assert(m_RadianResourceBrowerWnd);
	m_RadianResourceBrowerWnd->setVisible(true);
	m_RadianResourceBrowerWnd->show();
}
//--------------------------------------------------------------------------
void RadianEditor::switchToRealTimeMode()
{
	RadianEditorEngine::getSingleton().SwitchRealTimeMode();
}
//--------------------------------------------------------------------------
